﻿using System;
using System.ComponentModel;

namespace Infrastructure.Tools
{
    public class BackgroundResult : IResult
    {
        private readonly Action action;

        public BackgroundResult(Action action)
        {
            this.action = action;
        }

        public void Execute()
        {
            var backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += (e, sender) => action();
            backgroundWorker.RunWorkerCompleted += (e, sender) => Completed(this, EventArgs.Empty);
            backgroundWorker.RunWorkerAsync();
        }

        public event EventHandler Completed;
    }
}